package com.fyzk.activity.lottery.shuzicai;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.down_numhistory.NumHistoryPrizeView;
import com.fyzk.activity.lottery.basketball.JCLQSelectPlayMethodPop;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper.BallClickListener;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper.BallItemClickLitener;
import com.fyzk.activity.myview.BallPopFrameLayout;
import com.fyzk.activity.myview.BallScrollView;
import com.fyzk.activity.myview.DrawBallFrameLayout;
import com.fyzk.activity.myview.WeiZhi;
import com.fyzk.bean.PrizeHistoryItem;
import com.fyzk.betzhushu.ZhuShuFactory;
import com.tyjc.lottery.R;
import com.fyzk.random.RandomFactory;
import com.fyzk.utils.HelpUrlConstants;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.TopTitleView;
import com.fyzk.view.ViewCallBack;

import java.util.ArrayList;
import java.util.List;


public class QLCActivity extends LotteryBaseActivity implements
		BallClickListener, BallItemClickLitener, JCLQSelectPlayMethodPop.OnWFCheckChangedListener {

	private DrawBallFrameLayout ball_linear;
	private BallScrollView ball_scroll;

	private BallPopFrameLayout moveQiu;

	private TopTitleView topView;

	private SelectBallView ballView_firstLine;
	private SelectBallView ballView_twoline;

	private TextView bottom_zhushu;
	private TextView bottom_money;

	private LinearLayout dan_ll;
	private TextView first_tishi;

	private TextView ballTitle_t_h, hong_tishi,ballTitle_d;
	private LinearLayout yao_ll, ll_history;

	private PopSSQSelectPlayMethod popSSQSelectPlayMethod;

	private final String[] pop_titles = { "直选", "胆拖", "介绍" };
	private int wanFaId = LotteryIDUtils.QLC_LOTTERY_COMMON_DAN_ID_INT;
	protected int hong_count = 30;
	List<BallInfo> balls_firstLine = new ArrayList<BallInfo>();
	List<BallInfo> balls_twoLine = new ArrayList<BallInfo>();

	private final int DAN_MAX = 6;
	private final int TUO_MAX = 21;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dj_activity_qlc);

		initBallView();
		ViewCallBack.getInstance().AttachCountDownObj(this);

//		popupClickItem(popupId);
//		ballClick();
	}

	@Override
	public TopTitleView getTopView() {
		return topView;
	}

	private void initBallView() {
		// TODO Auto-generated method stub
		TouZhuHelper.getInstance().setBallClickListener(this);
		TouZhuHelper.getInstance().setBallItemClickLitener(this);

		topView = (TopTitleView) findViewById(R.id.topTitle);
		topView.setRightVisible(View.VISIBLE);
		topView.setCenterText(R.string.qlc);
		topView.setJianTouVisible(View.VISIBLE);
		topView.setOnTitleClickListener(this);

		popSSQSelectPlayMethod = new PopSSQSelectPlayMethod(this,
				AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
		popSSQSelectPlayMethod.setOnWFCheckChangedListener(this);

		moveQiu = (BallPopFrameLayout) findViewById(R.id.pl5_fl_moveqiu);
		ball_linear = (DrawBallFrameLayout) findViewById(R.id.pl5_drawball_framelayout);
		ball_scroll = (BallScrollView) findViewById(R.id.pl5_ballscroll);
		ball_linear.setScr(ball_scroll);
		ball_linear.setFl_moveqiu(moveQiu);
		ball_linear.setUpOrDownListener(this);
		ballView_firstLine = (SelectBallView) findViewById(R.id.firstLine);
		ballView_twoline = (SelectBallView) findViewById(R.id.twoLine);
		bottom_zhushu = (TextView) findViewById(R.id.pl5_bottom_tv_zhushu);
		bottom_money = (TextView) findViewById(R.id.pl5_bottom_tv_money);

		dan_ll = (LinearLayout) findViewById(R.id.qlc_dan_ll);
		ballTitle_t_h = (TextView) findViewById(R.id.qlc_balltitle_t_h);
		ballTitle_d = (TextView) findViewById(R.id.qlc_balltitle_d);
		hong_tishi = (TextView) findViewById(R.id.qlc_second_tishi);
		first_tishi = (TextView) findViewById(R.id.qlc_first_tishi);
		yao_ll = (LinearLayout) findViewById(R.id.qlc_yao_ll);
		yao_ll.setOnClickListener(this);
		// 新加
		ll_history = (LinearLayout) findViewById(R.id.qlc_ball_history);

		initArrayInfos();

		ballView_firstLine.setSelect_max(DAN_MAX);// 胆拖最多可选6个球
		ballView_twoline.setSelect_max(TUO_MAX);// 胆拖最多可选21个球

		// 设置关联选球SelectView
		ballView_firstLine.setGuanLian_view(ballView_twoline);
		ballView_twoline.setGuanLian_view(ballView_firstLine);

		initBottomView();
	}

	@Override
	protected void initArrayInfos() {
		if (arr_infos.size() == 2) {
			balls_firstLine = arr_infos.get(0);
			balls_twoLine = arr_infos.get(1);
		} else {
			arr_infos.clear();
			initBallList(balls_firstLine, hong_count, 1,true);
			initBallList(balls_twoLine, hong_count, 1,true);
			arr_infos.add(balls_firstLine);
			arr_infos.add(balls_twoLine);
		}

		ballView_firstLine.setBallInfoList(balls_firstLine);
		ballView_twoline.setBallInfoList(balls_twoLine);
	}


	@Override
	public void onRightClick() {
		// TODO Auto-generated method stub
		super.onRightClick();
		toHelp();
//		initPop();
//		if (popClick.isShowing()) {
//			popClick.dismiss();
//		} else {
//			popClick.showAsDropDown(topView.getRightView());
//		}
	}

	@Override
	public boolean touZhu() {
		// TODO Auto-generated method stub
		long zhuShu = getZhushu();
		if (zhuShu > 0) {
			int lotteryId = wanFaId;
			if (wanFaId != LotteryIDUtils.QLC_LOTTERY_TOWED_ID_INT) {
				lotteryId = zhuShu > 1 ? LotteryIDUtils.QLC_LOTTERY_COMMON_FU_ID_INT
						: LotteryIDUtils.QLC_LOTTERY_COMMON_DAN_ID_INT;
			}
			touZhu1(lotteryId, zhuShu);

			return true;
		} else {
			return false;
		}
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		bottom_zhushu.setText(0 + "注 ");
		bottom_money.setText("共" + 0 + "米");

		initBallList(balls_firstLine, hong_count, 1,true);
		initBallList(balls_twoLine, hong_count, 1,true);

		ballView_firstLine.setSelect_count(0);
		ballView_twoline.setSelect_count(0);

		ballView_firstLine.invalidate();
		ballView_twoline.invalidate();
	}

	@Override
	public int getLotteryId() {
		// TODO Auto-generated method stub
		return LotteryIDUtils.QLC_LOTTERY_ID_INT;
	}

	@Override
	protected void popupClickItem(int arg2) {
		// TODO Auto-generated method stub
		popupId = arg2;
		popSSQSelectPlayMethod.setCheckItem(arg2);
		switch (arg2) {
		case 0:
			onWFCheckChange(R.id.cb_selectplaymethod_normal_ssq, false);
			break;
		case 1:
			onWFCheckChange(R.id.cb_selectplaymethod_array_ssq, false);
			break;
		default:
			break;
		}
	}

	@Override
	protected void addHistoryView(ArrayList<PrizeHistoryItem> historyItems,
			boolean isWaitting) {
		// TODO Auto-generated method stub
		ll_history.removeAllViews();
		ll_history.addView(new NumHistoryPrizeView(getContext(), historyItems).getView());
	}

	@Override
	public void randomBallNum() {
		// TODO Auto-generated method stub
		int two[] = RandomFactory.getRandom(getLotteryId()).randomDanHang(7);

		initBallList(balls_twoLine, hong_count, 1,true);

		for (int i = 0; i < two.length; i++) {
			balls_twoLine.get(two[i]).setSelected(true);
		}

		ballView_twoline.invalidate();

		ballClick();
	}

	@Override
	public String getHelpUrl() {
		// TODO Auto-generated method stub
		return HelpUrlConstants.Help_QLC;
	}

	@Override
	public String getHelpTitle() {
		// TODO Auto-generated method stub
		return getString(R.string.help_qlc);
	}


	@Override
	public void ballClick(WeiZhi wz) {
		// TODO Auto-generated method stub
		WeiZhi w = wz;
		// TODO Auto-generated method stub
		int left = w.getLeft() - 5;
		int right = w.getRight() + 5;
		int top = (int) ((ball_linear.getY1() - w.getPointY() + w.getTop())
				- (w.getBottom() - w.getTop()) - 10);
		int bottom = (int) (ball_linear.getY1() - w.getPointY() + w.getTop());
		Rect rect = new Rect(left, top, right, bottom);
		moveQiu.setRect(rect, w.getNum(), w.getType(), w.isShowMoveBall);
	}

	@Override
	public void onCenterClick() {
		super.onCenterClick();
		popSSQSelectPlayMethod.showAsDropDown(topView);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()){
			case R.id.qlc_yao_ll:
				randomBallNum();
				break;
		}
	}

	@Override
	public void ballClick() {
		// TODO Auto-generated method stub
		long zhushu = getZhushu();
		long money = zhushu * 2;
		bottom_zhushu.setText(zhushu + "注 ");
		bottom_money.setText("共" + money + "米");
	}

	private long getZhushu() {
		return ZhuShuFactory.getZhuShu(wanFaId).betZhuShu(arr_infos);
	}

	@Override
	public void onWFCheckChange(int id, boolean isChecked) {
		clear();
		switch (id){
			case R.id.cb_selectplaymethod_normal_ssq:
				yao_ll.setVisibility(View.VISIBLE);
				isYao = true;
				topView.setCenterText(R.string.qlc);
				dan_ll.setVisibility(View.GONE);
				hong_tishi.setText(R.string.qlc_tishi);
				ballTitle_t_h.setText("选球区");
				wanFaId = LotteryIDUtils.QLC_LOTTERY_ID_INT;
				popupId = 0;
				break;
			case R.id.cb_selectplaymethod_array_ssq:
				yao_ll.setVisibility(View.INVISIBLE);
				isYao = false;
				topView.setCenterText(R.string.qlc_dantuo);
				dan_ll.setVisibility(View.VISIBLE);
				ballTitle_t_h.setText("拖码");
				ballTitle_d.setText("胆码");
				first_tishi.setText(R.string.qlc_dan);
				hong_tishi.setText(getString(R.string.qlc_tuo));
				wanFaId = LotteryIDUtils.QLC_LOTTERY_TOWED_ID_INT;
				popupId = 1;
				break;
		}
		popSSQSelectPlayMethod.dismiss();
	}

	@Override
	protected void addMissValue() {
		super.addMissValue();
		try {
			if (missInfo != null) {
				ballView_firstLine.setMissValues(missInfo.missValue("1003MV_X").get(0));
				ballView_twoline.setMissValues(missInfo.missValue("1003MV_X").get(0));
			}
		} catch (Exception e) {

		}


	}
}
